package com.shujia.day12;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo04IOWriteTxt {
    public static void main(String[] args) {
        String path = "data/data.txt";
        String outPath = "data/new_data.txt";
        int capacityLen = 10;

        try {
            byte[] bytes = new byte[capacityLen];
            FileInputStream fileInputStream = new FileInputStream(path);
            FileOutputStream fileOutputStream = new FileOutputStream(outPath);
            int readLen = 0;
            while ((readLen = fileInputStream.read(bytes))>0){
                fileOutputStream.write(bytes,0,readLen);
                fileOutputStream.flush(); // 每次获取到数据写入文件后，都会进行刷写操作 将内存中的数据直接写入 避免因为程序宕机 导致数据丢失
            }

        } catch (FileNotFoundException e) {
            System.err.println("文件找不到错误...");
        } catch (IOException e) {
            System.err.println("数据读取错误...");
        }



    }
}

